package com.lucky.ebidding.service;

import com.lucky.ebidding.dao.NotificationDao;
import com.lucky.ebidding.dao.ZbInfoDao;
import com.lucky.ebidding.dto.NotificationDTO;
import com.lucky.ebidding.enums.NotificationStatusEnum;
import com.lucky.ebidding.enums.NotificationTypeEnum;
import com.lucky.ebidding.model.Notification;
import com.lucky.ebidding.model.NotificationExample;
import com.lucky.ebidding.model.ZbInfo;
import com.lucky.ebidding.service.interfaces.NotificationServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:NotificationService
 * Package:com.lucky.ebidding.service
 * Description:
 *
 * @Date:2020/5/2 15:05
 * @Author:857251389@qq.com
 */
@Service
public class NotificationService implements NotificationServiceI {

    @Autowired
    private NotificationDao notificationDao;

    @Autowired
    private ZbInfoDao zbInfoDao;


    @Override
    public void sendNotification(Integer receiver, Integer notificator, Integer type, Integer zbId, String... args) {

        Notification notification = new Notification();
        notification.setNotificator(notificator);
        notification.setReceiver(receiver);
        notification.setType(type);
        notification.setContent(args[0]);
        notification.setZbid(zbId);
        notificationDao.insertSelective(notification);
    }

    @Override
    public void sendShNotification(Integer receiver, Integer notificator, Integer type, String... args) {
        Notification notification = new Notification();
        notification.setNotificator(notificator);
        notification.setReceiver(receiver);
        notification.setType(type);
        notification.setContent(args[0]);
        notificationDao.insertSelective(notification);
    }

    @Override
    public List<NotificationDTO> selectNotificationByReceiver(Integer receiver, Integer status) {
        NotificationExample notificationExample = new NotificationExample();
        notificationExample.createCriteria().andReceiverEqualTo(receiver).andStatusEqualTo(status);
        List<Notification> notifications = notificationDao.selectByExample(notificationExample);

        List<NotificationDTO> notificationDTOS = new ArrayList<>();

        for (Notification notification : notifications) {
            NotificationDTO notificationDTO = new NotificationDTO();
            ZbInfo zbInfo = zbInfoDao.selectByPrimaryKey(notification.getZbid());
            notificationDTO.setNotification(notification);
            notificationDTO.setZbInfo(zbInfo);
            notificationDTO.setTypeName(NotificationTypeEnum.nameOfType(notification.getType()));
            notificationDTOS.add(notificationDTO);
        }
        return notificationDTOS;
    }

    @Override
    public int setStatusRead(Integer notificationId) {
        Notification notification = new Notification();
        notification.setId(notificationId);
        notification.setStatus(1);
        int i = notificationDao.updateByPrimaryKeySelective(notification);
        return i;
    }
}
